<?
	session_start();
	ob_start();
	//global $TRDF;
	if(empty($_SESSION['user_login'])){
		echo"
		<br><br><br><br><br>
		<div align=center style='font-family: verdana; font-size: 10px; color: red;'>
		 Maaf, Halaman ini tidak bisa diakses. <br>
		 <a href='../index.php'>Anda harus login terlebih dahulu.</a><br><br><BR>
		 <span style='color:black;'>Karyawan Dot Net</span>
		</div>
		";
		exit();
	}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Report:: Medical Restitusi</title>
<style type="text/css">
 body,table{
 	font-family:Verdana, Arial, Helvetica, sans-serif;
	font-size: 11px;
 }
 td{
	font-size: 10px;
 }
</style>
<link rel="stylesheet" type="text/css" href="../style/print.css" media="print" />

<style type="text/css">
 .white_text{
 	background-color:#FFFFFF;
	color:#000000;
 }
</style>
</head>
<?
	$periode 	= $HTTP_GET_VARS['periode'];
	$jenis 		= $HTTP_GET_VARS['jenis'];
	$awal		= $HTTP_GET_VARS['awal'];
	// dd-mm-yyyy
	// 0123456789
	$d = substr($awal,0,2);
	$m = substr($awal,3,2);
	$y = substr($awal,6,4);
	
	$awal 		= $y."-".$m."-".$d;
	
	$akhir		= $HTTP_GET_VARS['akhir'];
	$dd = substr($akhir,0,2);
	$mm = substr($akhir,3,2);
	$yy = substr($akhir,6,4);
	$akhir 		= $yy."-".$mm."-".$dd;

	$divisi		= $HTTP_GET_VARS['division'];
	$keyword	= $HTTP_GET_VARS['keyword'];
	
	include("../lib/config.php");
	
// cek hak akses untuk cabang
	$sad = mssql_query("SELECT nama_menu FROM VPrivUser A, VCabang B WHERE username='".$HTTP_SESSION_VARS['user_login']."' AND baca='1' AND A.nama_menu=B.Kode_Cabang",$conn);
	while($rad = mssql_fetch_array($sad)){
	// VARIABLE UNTUK CABANG
	$VAreaCode[] = $rad[0];
	}
///////
	
	function CallMedical($conn,$awal,$akhir,$MA,$nik,$jenis){
		$sql = "select A.*, convert(char(12), tanggal, 103) tgl_klaim, convert(char(12), tgl_bayar, 103) tglbayar, ";
		$sql .= " jml_diajukan-jml_disetujui jml_ditolak from medicalrestitusidetail A, vlatestkarir B ";
		$sql .= " where A.NIK=B.NIK and B.NIK='".$nik."' ";

		if(!empty($awal) && !empty($akhir)){
			$sql .= " and (A.tanggal between '".$awal."' and '".$akhir."') ";
		}

		$sql .= $MA." order by tanggal asc";
		
		//echo $ss;
		$qq = mssql_query($sql,$conn);
		while($rr = mssql_fetch_array($qq)){
			echo"
			 <tr>
			  <td>$rr[tgl_klaim]</td>
			  <td>$rr[tglbayar]</td>
			  <td>$rr[nama_tertanggung] - $rr[status_kel]</td>
			  <td>$rr[nama_dokter]</td>
			  <td align=right>".number_format($rr[jml_diajukan])."</td>
			  <td align=right>".number_format($rr[jml_disetujui])."</td>
			  <td align=right>";
			
			if($rr[jml_ditolak] == 0){
				echo"&nbsp;";
			}
			else{ 
				echo number_format($rr[jml_ditolak]);
			}
			
			echo"</td>
			  <td>$rr[Remarks]</td>
			 </tr>
			";
		}
		// jumlah total pengobatan X
		switch($jenis){
			case "RJ":
				$MSG = "<b>Biaya Pengobatan &amp; Rawat Jalan</b>";
				break;
			case "RI":
				$MSG = "<b>Biaya Rawat Inap</b>";
				break;
			case "KC":
				$MSG = "<b>Biaya Kacamata</b>";
				break;
		}
		
		$sqlT = "select sum(jml_diajukan) KLAIM, sum(jml_disetujui) DIBAYAR, sum(jml_diajukan)-sum(jml_disetujui) DITOLAK from medicalrestitusidetail A, vlatestkarir B where A.NIK=B.NIK and B.NIK='".$nik."' ";

		if(!empty($awal) && !empty($akhir)){
			$sqlT .= " and (A.tanggal between '".$awal."' and '".$akhir."') ";
		}

		$sqlT .= $MA;

		$qqT = mssql_query($sqlT,$conn);
		$rrT = mssql_fetch_array($qqT);
		echo"
		<tr>
		 <td colspan=2 style=\"border-bottom:solid 1px #000000;\">$MSG</td>
		 <td colspan=2 style=\"border-bottom:solid 1px #000000;\" align=right><b>";
		if($jenis == "RJ"){
		echo"SALDO : ";
		}
		else{
		echo"&nbsp;";
		}
		echo"</b></td>
		 <td align=right style=\"border-bottom:solid 1px #000000;\"><b>".number_format($rrT[KLAIM])."</td>
		 <td align=right style=\"border-bottom:solid 1px #000000;\"><b>".number_format($rrT[DIBAYAR])."</td>
		 <td align=right style=\"border-bottom:solid 1px #000000;\"><b>";
		 if(number_format($rrT[DITOLAK]) == 0){
		 echo"&nbsp;";
		 }
		 else{
		 	echo number_format($rrT[DITOLAK]);
		 }
		 echo"</td>
		 <td style=\"border-bottom:solid 1px #000000;\">&nbsp;</td>
		</tr>
		<tr>
		 <td colspan=8>&nbsp;</td>
		</tr>
		";
	}
?>
<body>
<?	
 	$s = "select B.NIK,B.Nama,kode_so2,jabatan2 from medicalrestitusidetail A, vlatestkarir B, ";
	$s .= "vlevelcabang C where A.NIK=B.NIK ";
	$s .= " AND B.kode_so2=C.kode_so ";		
	
	if($divisi != "ALL"){
	$s .= " AND substring(B.kode_so2,1,7)='".substr($divisi,0,7)."' ";
	}
	
	$s .= " AND C.AreaCode IN (";

	for($k=0;$k<count($VAreaCode);$k++){
		$s .= "'$VAreaCode[$k]',";
	}

	$s .= "'$VAreaCode[0]') ";		
	
	if(!empty($awal) && !empty($akhir)){
		$s .= " and tanggal between '$awal' and '$akhir' ";
	}

	//if(!empty($divisi)){
	//	$s .= " and substring(kode_so2,1,7)='".substr($divisi,0,7)."' ";
	//}
	
	if(!empty($keyword)){
		$s .= " and (B.Nama like '%$keyword%' or B.NIK like '%$keyword%') ";
	}
		
	$s .= " group by B.NIK,B.Nama,kode_so2,jabatan2 order by B.Nama asc";
	
	$q = mssql_query($s,$conn);
	
	if(!mssql_num_rows($q)){
		echo"<br><br><br><br><br><div align=center>DATA RESTITUSI DETAIL TIDAK ADA ...</div>";
		exit;
	}
	
	while($r = mssql_fetch_array($q)){
 ?>
<table width="100%" border="0" style='page-break-after:always;'>
 <tr>
  <td align="left" style="font-size:14px;">KOPERASI KARYAWAN<BR />
  Data Klaim Biaya Pengobatan, Kacamata, dan Rawat Inap
  </td>
 </tr>
 <tr>
  <td align="left" style="font-size:14px;">
  </td>
 </tr>
 <tr>
  <td>Periode: <?=$HTTP_GET_VARS['awal']?> - <?=$HTTP_GET_VARS['akhir']?></td>
 </tr>
 <tr>
  <td align="right">Tgl Cetak: <?=date("d/m/Y")?></td>
 </tr> 
 <tr>
  <td>
   <table>
    <tr>
     <td>NIK</td>
     <td width="200"><?=$r[NIK]?></td>
     <td>&nbsp;</td>
     <td>Jabatan</td>
     <td>
	 <?
     	if(!empty($r[jabatan2])){
			$x = mssql_query("select jabatan from MstJabatan where kode='".$r[jabatan2]."'",$conn);
			$y = mssql_fetch_array($x);
			echo $y[0];
		}
	 ?></td>
    </tr>
    <tr>
     <td>Nama</td>
     <td><?=$r[Nama]?></td>
     <td>&nbsp;</td>
     <td>Divisi</td>
     <td>
	 <?
     	if(!empty($r[kode_so2])){
			$xx = mssql_query("select uraian from mstso where substring(kode_so,1,7)='".substr($r[kode_so2],0,7)."'",$conn);
			$yy = mssql_fetch_array($xx);
			echo $yy[0];
		}
	 ?></td>
    </tr>
   </table>
  </td>
 </tr>
 <tr>
  <td>
   <table width="100%" cellspacing="0" cellpadding="1">
    <tr>
     <td style="border-top:solid 1px #000000; border-bottom:solid 1px #000000;">Tgl Klaim</td>
     <td style="border-top:solid 1px #000000; border-bottom:solid 1px #000000;">Tgl Invoice</td>
     <td style="border-top:solid 1px #000000; border-bottom:solid 1px #000000;">Nama Pasien - Hub. Keluarga</td>
     <td style="border-top:solid 1px #000000; border-bottom:solid 1px #000000;">Rumah Sakit</td>
     <td style="border-top:solid 1px #000000; border-bottom:solid 1px #000000;" align="right">Klaim</td>
     <td style="border-top:solid 1px #000000; border-bottom:solid 1px #000000;" align="right">Dibayar</td>
     <td style="border-top:solid 1px #000000; border-bottom:solid 1px #000000;" align="right">Ditolak</td>
     <td style="border-top:solid 1px #000000; border-bottom:solid 1px #000000;">Keterangan</td>
    </tr>
    <?
	$nik 	= $r[NIK];
	// RAWAT JALAN
	$MA 	= " AND mata_anggaran IN ('811101','811103','811199') ";
	$jenis 	= "RJ";
    CallMedical($conn,$awal,$akhir,$MA,$nik,$jenis);
	// RAWAT INAP
	$MA 	= " AND mata_anggaran IN ('811102','811106') ";
	$jenis 	= "RI";
    CallMedical($conn,$awal,$akhir,$MA,$nik,$jenis);
	// KACAMATA
	$MA 	= " AND mata_anggaran IN ('811104') ";
	$jenis 	= "KC";
    CallMedical($conn,$awal,$akhir,$MA,$nik,$jenis);
	
	// total semuanya
	$stot = "select sum(jml_diajukan) total_klaim, sum(jml_disetujui) total_dibayar, sum(jml_diajukan)-sum(jml_disetujui) total_ditolak from medicalrestitusidetail A, vlatestkarir B where A.NIK=B.NIK and B.NIK='".$nik."' ";
 
	if(!empty($awal) && !empty($akhir)){
		$stot .= " and (A.tanggal between '".$awal."' and '".$akhir."') ";
	}

	$qtot = mssql_query($stot,$conn);
	$rtot = mssql_fetch_array($qtot);
	?>
    <tr>
     <td colspan="4" align="center"><b>TOTAL</td>
     <td align="right"><b><?=number_format($rtot[total_klaim])?></td>
     <td align="right"><b><?=number_format($rtot[total_dibayar])?></td>
     <td align="right"><b><?
     if(number_format($rtot[total_ditolak]) == 0){
	 }
	 else{
	 	echo number_format($rtot[total_ditolak]);
	 }
	 ?></td>
    </tr>
   </table>
  </td>
 </tr>
 <tr>
  <td>&nbsp;</td>
 </tr>
</table>
<?
 	}

// export to excel
if($HTTP_GET_VARS['excel'] == "yes"){
	header("Content-type: application/vnd.ms-excel");
	header("Content-Disposition: attachment; filename=LaporanPengobatanDetail.xls");
	header("Pragma: no-cache");
	header("Expires: 0");
}
?>
</body>
</html>
